package com.cn.serverline.lineicp.workshopPlan.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.roses.kernel.office.api.pojo.report.ExcelExportParam;
import cn.stylefeng.roses.kernel.office.excel.OfficeExcel;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.serverline.lineicp.workshopPlan.entity.PlanLineAutomaticTasks;
import com.cn.serverline.lineicp.workshopPlan.entity.request.ConfigurationConfigResponse;
import com.cn.serverline.lineicp.workshopPlan.entity.request.LineAssembleBatchSplitRequest;
import com.cn.serverline.lineicp.workshopPlan.entity.request.LineAutomaticTasksExcel;
import com.cn.serverline.lineicp.workshopPlan.entity.request.LineAutomaticTasksRequest;
import com.cn.serverline.lineicp.workshopPlan.enums.LinePlanEnum;
import com.cn.serverline.lineicp.workshopPlan.enums.MaterialStatisticsEnum;
import com.cn.serverline.lineicp.workshopPlan.mapper.PlanLineAutomaticTasksMapper;
import com.cn.serverline.lineicp.workshopPlan.service.IPlanLineAutomaticTasksService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 周总排产结果表 服务实现类
 * </p>
 *
 * @author 田江余
 * @since 2024-08-11
 */
@Service("PlanLineAutomaticTasksServiceImpl")
public class PlanLineAutomaticTasksServiceImpl extends ServiceImpl<PlanLineAutomaticTasksMapper, PlanLineAutomaticTasks> implements IPlanLineAutomaticTasksService {

    @Resource
    private LineAssembleBatchSplitServiceImpl lineAssembleBatchSplitService;

    @Resource
    private OfficeExcel officeExcel;

    /**
     * 自动排产
     * @param planLineAutomaticTasks 请求参数
     */
    @Override
    public void add(PlanLineAutomaticTasks planLineAutomaticTasks) {
        this.save(planLineAutomaticTasks);
    }

    /**
     * 撤回排产
     * @param planLineAutomaticTasks 请求参数
     */
    @Override
    public void del(PlanLineAutomaticTasks planLineAutomaticTasks) {
        this.removeById(planLineAutomaticTasks);
    }

    @Override
    public void edit(PlanLineAutomaticTasks planLineAutomaticTasks) {
        this.updateById(planLineAutomaticTasks);
    }

    @Override
    public PlanLineAutomaticTasks detail(PlanLineAutomaticTasks planLineAutomaticTasks) {
        return this.getById(planLineAutomaticTasks);
    }

    @Override
    public PageResult<PlanLineAutomaticTasks> findPage(PlanLineAutomaticTasks planLineAutomaticTasks) {
        LambdaQueryWrapper<PlanLineAutomaticTasks> wrapper = new LambdaQueryWrapper<>();
        // 筛选主要属性
        /* wrapper.select(PlanLineAutomaticTasks::getId, PlanLineAutomaticTasks::getPositionName, PlanLineAutomaticTasks::getCode, PlanLineAutomaticTasks::getRemark,
        PlanLineAutomaticTasks::getPositionSort, BaseEntity::getCreateTime);*/

        Page<PlanLineAutomaticTasks> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    /**
     * 周总排产结果表列表
     * @param lineAutomaticTasksRequest 请求参数
     * @return 返回数据列表
     */
    @Override
    public HashMap<String, Object> findList(LineAutomaticTasksRequest lineAutomaticTasksRequest) {
        HashMap<String, Object> map = new HashMap<>();

        if (lineAutomaticTasksRequest.getStartTime() != null && lineAutomaticTasksRequest.getEndTime() != null){
            String startTime = DateUtil.format(lineAutomaticTasksRequest.getStartTime(), "yyyy-MM-dd");
            String endTime = DateUtil.format(lineAutomaticTasksRequest.getEndTime(), "yyyy-MM-dd");
            lineAutomaticTasksRequest.setStartDateString(startTime);
            lineAutomaticTasksRequest.setEndDateString(endTime);

        }

        //拆分集合
        List<List<Long>> arrayTasksIds = Lists.partition(lineAutomaticTasksRequest.getTasksIds(), MaterialStatisticsEnum.oneThousand);
        lineAutomaticTasksRequest.setArrayTasksIds(arrayTasksIds);
        List<LineAutomaticTasksRequest> list = baseMapper.finalAssemblyResult(lineAutomaticTasksRequest);
        map.put("data",list);
        //获取最早任务开始时间
        List<Timestamp> startTimes = list.stream().map(LineAutomaticTasksRequest::getTaskStartTime).sorted().collect(Collectors.toList());
        if(startTimes.size() > 0){
            map.put("earliestStartTime",startTimes.get(0));
        }else {
            map.put("earliestStartTime","");
        }
        //获取最晚任务结束时间
        List<Timestamp> endTimes = list.stream().map(LineAutomaticTasksRequest::getTaskEndTime).sorted().collect(Collectors.toList());
        if (endTimes.size() > 0){
            map.put("latestEndTime",endTimes.get(endTimes.size()-1));
        }else {
            map.put("latestEndTime","");
        }
        return map;
    }

    /**
     * 导出excel
     * @param lineAutomaticTasksRequest 请求参数
     * @param response 响应
     */
    @Override
    public void download(LineAutomaticTasksRequest lineAutomaticTasksRequest, HttpServletResponse response) {
        HashMap<String, Object> map = findList(lineAutomaticTasksRequest);
        List<LineAutomaticTasksRequest> list = (List<LineAutomaticTasksRequest>) map.get("data");
        List<LineAutomaticTasksExcel> lineAutomaticTasksExcels = BeanUtil.copyToList(list, LineAutomaticTasksExcel.class);

        ExcelExportParam excelExportParam = new ExcelExportParam();
        excelExportParam.setClazz(LineAutomaticTasksExcel.class);
        //工作簿名称
        excelExportParam.setSheetName("总装作业排产结果");
        //下载提示的文件名m
        excelExportParam.setFileName("总装作业排产结果");
        excelExportParam.setDataList(lineAutomaticTasksExcels);
        //Excel类型
        excelExportParam.setExcelTypeEnum(ExcelTypeEnum.XLSX);
        excelExportParam.setResponse(response);
        //文件写入绝对路径 写入到服务器磁盘用
        officeExcel.easyExportDownload(excelExportParam);
    }

}
